import 'package:custom_widgets/item/item_normal_widget.dart';
import 'package:flutter/material.dart';
import 'package:fluwx/fluwx.dart';
import 'package:mi_mang_flutter/config/configs.dart';
import 'package:mi_mang_flutter/page/setting/setting_router.dart';
import 'package:mi_mang_flutter/provider/app_theme.dart';
import 'package:mi_mang_flutter/res/dimens.dart';
import 'package:mi_mang_flutter/res/styles.dart';
import 'package:mi_mang_flutter/routers/fluro_navigator.dart';
import 'package:mi_mang_flutter/utils/dialog_util.dart';
import 'package:mi_mang_flutter/utils/image_utils.dart';
import 'package:mi_mang_flutter/utils/locale_util.dart';
import 'package:mi_mang_flutter/utils/utils.dart';
import 'package:package_info_plus/package_info_plus.dart';

class AboutPage extends StatefulWidget {
  @override
  _AboutPageState createState() => _AboutPageState();
}

class _AboutPageState extends State<AboutPage> {
  // 是否显示小版本号
  bool _showBuild = false;
  // 版本号
  String? _version;
  // 小版本号
  String? _buildNumber;

  @override
  void initState() {
    super.initState();
    _initPackageInfo();
  }

  /// 获取版本
  _initPackageInfo() {
    PackageInfo.fromPlatform().then((PackageInfo packageInfo) {
      String version = packageInfo.version;
      String buildNumber = packageInfo.buildNumber;
      setState(() {
        _version = version;
        _buildNumber = buildNumber;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(i18n(context).about),
        centerTitle: true,
      ),
      body: SafeArea(
        child: _buildBody(),
      ),
    );
  }

  Widget _buildBody() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        _buildTop(),
        ...ItemNormalWidget.divideTiles(childs: <Widget>[
          ItemNormalWidget.item(
            title: i18n(context).featuresIntroduction,
            onTap: () {
              NavigatorUtils.push(context, SettingRouter.introduce);
            },
          ),
          ItemNormalWidget.item(
            title: i18n(context).recommend,
            onTap: () {
              // 推荐朋友
              var shareModel = WeChatShareWebPageModel(
                "https://www.yymimang.cn/mimang/app/",
                title: "米茫",
                // thumbDataHash:
                //     "https://mmbiz.qlogo.cn/mmbiz_png/iaibOW5jlcZGwtia8FK1QkAxPgtDGW8Uehj5THopStcYkBfphLiacg9gImReMyov8suRG02wZSfubGiaMUd8VHOEzsw/0?wx_fmt=png",
                description: "工作之余,可以看一些清新美图,让忙碌的生活得到一些放松,补充一些正能量。",
                messageExt: "yymimang",
              );
              DialogUtil.showShare(context, shareModel);
            },
          ),
          // ItemNormalWidget.item(
          //   title: i18n(context).checkForUpdates,
          //   onTap: () {},
          // ),
          ItemNormalWidget.item(
            title: i18n(context).contactUs,
            onTap: () {
              Utils.launchURL("mailto:yymimang@foxmail.com");
            },
          ),
          // ItemNormalWidget.item(
          //   title: i18n(context).supportMe,
          //   onTap: () {},
          // ),
        ]),
        Spacer(),
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            InkWell(
              borderRadius: BorderRadius.all(Radius.circular(2)),
              child: Text(
                "《${i18n(context).serviceAgreement}》",
                style: TextStyles.bodySmall?.copyWith(
                  fontSize: Dimens.fontSp13,
                  color: Color(0xFF52BBF4),
                ),
              ),
              onTap: () {
                var isDark = ThemeUtil.isDark(context);
                NavigatorUtils.goWebViewPage(
                  context,
                  i18n(context).serviceAgreement,
                  "${Configs.WEB_SERVICE_AGREEMENT}${isDark ? '_dark' : ''}/",
                );
              },
            ),
            Text(
              i18n(context).and,
              style: TextStyles.bodySmall?.copyWith(fontSize: Dimens.fontSp13),
            ),
            InkWell(
              borderRadius: BorderRadius.all(Radius.circular(2)),
              child: Text(
                "《${i18n(context).privacyPolicy}》",
                style: TextStyles.bodySmall?.copyWith(
                  fontSize: Dimens.fontSp13,
                  color: Color(0xFF52BBF4),
                ),
              ),
              onTap: () {
                var isDark = ThemeUtil.isDark(context);
                NavigatorUtils.goWebViewPage(
                  context,
                  i18n(context).privacyPolicy,
                  "${Configs.WEB_PRIVACY_POLICY}${isDark ? '_dark' : ''}/",
                );
              },
            ),
          ],
        ),
        Gaps.vGap15,
        Text(
          "${i18n(context).appName} ${i18n(context).copyright}",
          style: TextStyles.labelSmall,
        ),
        Gaps.vGap5,
        Text(
          "Copyright © 2019 Mimang.all Rights Reserved.",
          style: TextStyles.labelSmall?.copyWith(fontStyle: FontStyle.italic),
        ),
        Gaps.vGap15,
      ],
    );
  }

  /// 顶部布局
  Widget _buildTop() {
    return Padding(
      padding: EdgeInsets.only(top: 40.0, bottom: 40.0),
      child: Column(
        children: <Widget>[
          GestureDetector(
            onTap: () {
              setState(() {
                _showBuild = !_showBuild;
              });
            },
            child: Container(
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10.0),
                boxShadow: [
                  BoxShadow(
                    color: Colors.black45,
                    offset: Offset(1.0, 5.0),
                    blurRadius: 10.0,
                    spreadRadius: -2.0,
                  ),
                ],
              ),
              child: Material(
                borderRadius: BorderRadius.circular(10.0),
                child: ClipRRect(
                  borderRadius: BorderRadius.circular(10.0),
                  child: loadImage("icon_app", width: 100.0),
                ),
              ),
            ),
          ),
          Gaps.vGap20,
          Text(
            "米芒MiMang",
            style: TextStyles.titleMedium,
          ),
          Text(
            "version $_version${_showBuild ? "($_buildNumber)" : ""}${Configs.DEBUG ? '-Debug' : ''}",
            style: TextStyles.bodySmall,
          ),
        ],
      ),
    );
  }
}
